package com.study.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.study.dbo.ManagerDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

public class JWTCredentialsMatcher implements CredentialsMatcher {
	
	private final Logger log = LoggerFactory.getLogger(JWTCredentialsMatcher.class);

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        System.out.println("passswwwd");
        String token = (String) authenticationToken.getPrincipal();
        System.out.println(token);
        if (StringUtils.isBlank(token)){
            return false;
        }
        System.out.println("pass hculi");
        String account = JwtUtils.getUsername(token);
        System.out.println(account+"nnnn");
        try {
            Algorithm algorithm = Algorithm.HMAC256(JWTProperties.secret);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim(JWTProperties.claimKey, account)
                    .build();
            verifier.verify(token);
            System.out.println("asdhfuasf");
            return true;
        } catch (JWTVerificationException e) {
            log.error("Token Error:{}", e.getMessage());
            return false;
        }

    }

}
